package com.zelin.reference;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 用户登录线程
 */
public class LoginThread extends Thread{

    //客户端通信端点
    private Socket scocket;

    public LoginThread(){}
    public LoginThread(Socket scocket) {
        this.scocket = scocket;
    }

    public void run(){
        InputStream inputStream = null;
        ObjectInputStream ois = null;
        OutputStream outputStream = null;
        try{
            //读取客户端发送的内容
            inputStream = scocket.getInputStream();
            ois = new ObjectInputStream(inputStream);
            //反序列化对象
            User user = (User) ois.readObject();

            //发送消息到客户端
            outputStream = scocket.getOutputStream();
            String info;
            if (user.getName().equals("admin") && user.getPassword().equals("abc")) {
                info = "登录成功~~";
            } else {
                info = "用户名或密码输入错误！";
            }
            outputStream.write(info.getBytes());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                assert outputStream != null;
                outputStream.flush();
                outputStream.close();
                ois.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
